package www.openteam.com.model.business;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.DeleteInfo;
import www.openteam.com.bean.DeleteWholeInfo;
import www.openteam.com.exception.ApiException;
import www.openteam.com.mvp.IModel;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2016/11/23.
 */

public class EditCustomOrderModel extends BaseModel {
    private boolean isInsert = false;
    private boolean isUpdate = false;
    private boolean isDelete = false;

    //更新子订单
    public boolean updateCustomOrderItemInfo(
            int id, String prodno
            , String wraptype, String paperinf
            , String senddate, String technicsinf
            , String printcolor, String paynumb
            , String othernumb, String price
            , String eprodno, final UpdateCustomOrderItemInfoOnListener updateCustomOrderItemInfoOnListener
    ) {
        httpApiService.updateCustomOrderItemInfo(id, prodno, wraptype, paperinf, senddate, technicsinf, printcolor, paynumb, othernumb, price, eprodno
        ).compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isUpdate = true;
                        updateCustomOrderItemInfoOnListener.updateCOIISuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isUpdate = false;
                        updateCustomOrderItemInfoOnListener.updateCOIIFail(e);
                    }
                });

        return isUpdate;
    }

    //新建子订单
    public boolean insertCustomOrderItemInfo(
            int id, String prodno
            , String wraptype, String paperinf
            , String senddate, String technicsinf
            , String printcolor, String paynumb
            , String othernumb, String price, final InsertCustomOrderItemInfoOnListener insertCustomOrderItemInfoOnListener

    ) {
        httpApiService.insertCustomOrderItemInfo(id, prodno, wraptype, paperinf, senddate, technicsinf, printcolor, paynumb, othernumb, price)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isInsert = true;
                        insertCustomOrderItemInfoOnListener.insertCOIISuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isInsert = false;
                        insertCustomOrderItemInfoOnListener.insertCOIIFail(e);
                    }
                });
        return isInsert;
    }

    //更新主订单
    public boolean updateCustomOrderMainInfo(
            int id, String rdsno,
            String custid, String userid,
            String moneytype, String paytime
            , String sendtype, String custorderid
            , String alreadlypay, String inputdate
            , String inputer, String orderamount
            , String remark, String orderdate
            , String sendaddress, final String istax
            , String taxrate, String invtype
            , String stype, final UpdateCustomOrderInfoOnListener updateCustomOrderInfoOnListener
    ) {
        httpApiService.updateCustomOrderMainInfo(id, rdsno, custid, userid, moneytype, paytime, sendtype, custorderid, alreadlypay, inputdate, inputer, orderamount, remark, orderdate, sendaddress, istax, taxrate, invtype, stype)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isUpdate = true;
                        updateCustomOrderInfoOnListener.updateCOISuccess(approvalStatu);

                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isUpdate = false;
                        updateCustomOrderInfoOnListener.updateCOIFail(e);
                    }
                });

        return isUpdate;
    }

    public boolean deleteCustomOrderItemInfo(int zbid, String prodno, final EditCustomOrderItemInfoOnListener editCustomOrderItemInfoOnListener) {
        httpApiService.deleteCustomOrderItemInfo(zbid, prodno)
                .compose(new CommonTransformer<DeleteInfo>())
                .subscribe(new CommonSubscriber<DeleteInfo>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(DeleteInfo deleteInfo) {
                        isDelete = true;
                        editCustomOrderItemInfoOnListener.deleteECOIISuccess(deleteInfo
                        );
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isDelete = false;
                        editCustomOrderItemInfoOnListener.deleteECOIIFail(e);
                    }
                });
        return false;
    }

    public boolean deleteCustomOrderInfo(int id, final EditCustomOrderInfoOnListener editCustomOrderInfoOnListener) {
        httpApiService.deleteCustomOrderInfo(id)
                .compose(new CommonTransformer<DeleteWholeInfo>())
                .subscribe(new CommonSubscriber<DeleteWholeInfo>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(DeleteWholeInfo deleteWholeInfo) {
                        isDelete = true;
                        editCustomOrderInfoOnListener.deleteECOISuccess(deleteWholeInfo);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isDelete = false;
                        editCustomOrderInfoOnListener.deleteECOIFail(e);
                    }
                });

        return isDelete;
    }


//    //插入子订单
    public interface InsertCustomOrderItemInfoOnListener {
        void insertCOIISuccess(ApprovalStatu approvalStatu);

        void insertCOIIFail(Throwable throwable);

    }

    //更新子订单
    public interface UpdateCustomOrderItemInfoOnListener {

        void updateCOIISuccess(ApprovalStatu updateState);

        void updateCOIIFail(Throwable throwable);
    }


    public interface EditCustomOrderItemInfoOnListener {
        void deleteECOIISuccess(DeleteInfo deleteInfo);

        void deleteECOIIFail(Throwable throwable);
    }

    public interface EditCustomOrderInfoOnListener {
        void deleteECOISuccess(DeleteWholeInfo deleteInfo);

        void deleteECOIFail(Throwable throwable);
    }

    //更新主订单
    public interface UpdateCustomOrderInfoOnListener {

        void updateCOISuccess(ApprovalStatu updateState);

        void updateCOIFail(Throwable throwable);
    }
}
